<%@page contentType="text/html" pageEncoding="UTF-8" import="org.apache.commons.httpclient.*, org.apache.commons.httpclient.methods.*"%>
<%!    static final HttpClient hc = new HttpClient();

    static {
        hc.getParams().setParameter(
                org.apache.commons.httpclient.params.HttpMethodParams.USER_AGENT,
                "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");
    }
%>
<%


    String text = request.getParameter("myword");
    if (text == null) {
        return;
    }
    String lookupURL = "http://www.google.com/search?q=" + text + "&tbs=dfn:1";
    GetMethod get = new GetMethod(lookupURL);
    hc.executeMethod(get);
    final String s = get.getResponseBodyAsString();
    //out.print(s);
    String resultTag = "Search Results</h2>";
    int begin = s.indexOf(resultTag);
    int end = s.indexOf("</div></li>", begin);
    if (end > 0) {
        int end2 = s.indexOf("</div></li>", end + 2);
        if (end2 <= 0) {
            end = end2;
        } else {
            end = end2;
        }
    }
    if (begin > 0 && end > 0) {
        String buffer = s.substring(begin + resultTag.length(), end + 11);
        out.println(buffer);
    }
%>
